package web.mb;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import metier.entities.Catalogue_prix;
import metier.entities.Client;
import metier.entities.Declaration;
import metier.entities.Offre;
import metier.entities.Paiement;
import metier.entities.StrategiePrix;
import metier.sess.ICalculEstimationLocal;
import metier.sess.IOffreLocal;
import metier.sess.IPaiementLocal;
import metier.sess.IStrategiePrixLocal;


@ManagedBean
@RequestScoped
public class PaimentMB {
	
	@EJB
	IStrategiePrixLocal metierStartegie;
	
	@EJB
	IOffreLocal metierOffre;
	
	
	@EJB
	IPaiementLocal metierPaiement;
	
	//tva est déduite selon le pays de l'utilisateur
	private Double TVA=(double) 2;
	private Double prepaid;
	private  Double desiredPrice;
	private Double Total;
	private Integer type=1;
	private Catalogue_prix s;
	private Double PreTotal;
	private Integer IdOffre;
	private Offre offre;
	
	//méthode de paiement
	private boolean paypal;
	private boolean visa;
	private boolean bankTransfer;
	private boolean cash;
	private boolean cheque;
	private boolean westernUnion;
	private boolean moneyGram;
	private boolean wireTransfer;
	
	@PostConstruct
	public void init()
	{
		
		
	
	
	 
	}
    
	
    


	public Offre getOffre() {
		return offre;
	}





	public void setOffre(Offre offre) {
		this.offre = offre;
	}





	public boolean isPaypal() {
		return paypal;
	}





	public void setPaypal(boolean paypal) {
		this.paypal = paypal;
	}





	public boolean isVisa() {
		return visa;
	}





	public void setVisa(boolean visa) {
		this.visa = visa;
	}





	public boolean isBankTransfer() {
		return bankTransfer;
	}





	public void setBankTransfer(boolean bankTransfer) {
		this.bankTransfer = bankTransfer;
	}





	public boolean isCash() {
		return cash;
	}





	public void setCash(boolean cash) {
		this.cash = cash;
	}





	public boolean isCheque() {
		return cheque;
	}





	public void setCheque(boolean cheque) {
		this.cheque = cheque;
	}





	public boolean isWesternUnion() {
		return westernUnion;
	}





	public void setWesternUnion(boolean westernUnion) {
		this.westernUnion = westernUnion;
	}





	public boolean isMoneyGram() {
		return moneyGram;
	}





	public void setMoneyGram(boolean moneyGram) {
		this.moneyGram = moneyGram;
	}





	public boolean isWireTransfer() {
		return wireTransfer;
	}





	public void setWireTransfer(boolean wireTransfer) {
		this.wireTransfer = wireTransfer;
	}





	public Integer getIdOffre() {
		return IdOffre;
	}



	public void setIdOffre(Integer idOffre) {
		IdOffre = idOffre;
	}





	public StrategiePrix getS() {
		return s;
	}



	public void setS(StrategiePrix s) {
		this.s = (Catalogue_prix) s;
	}



	public Double getTVA() {
	
		return TVA;
	}



	public void setTVA(Double tVA) {
		TVA = tVA;
	}



	public Double getPrepaid() {
		return prepaid;
	}



	public void setPrepaid(Double prepaid) {
		this.prepaid = prepaid;
	}



	public Double getDesiredPrice() {
		return desiredPrice;
	}



	public void setDesiredPrice(Double desiredPrice) {
		this.desiredPrice = desiredPrice;
	}



	public Double getTotal() {
		return Total;
	}



	public void setTotal(Double total) {
		Total = total;
	}


	public Integer getType() {
		return type;
	}


	public void setType(Integer type) {
		this.type = type;
	}


	public Double getPreTotal() {
		return PreTotal;
	}


	public void setPreTotal(Double preTotal) {
		PreTotal = preTotal;
	}


	public void setS(Catalogue_prix s) {
		this.s = s;
	}
	
	
	
   //liste des offres
	public List<Offre> listeOffre()
	{
		return metierOffre.getAllOffre();
	}
	
	public String  voir(Integer i)
	{
		
	   IdOffre=i;
		offre=metierOffre.getOffre(IdOffre);
		return  "InfoOffre.xhtml";
	}
	
	//action payer
	//i=0 pour le listing , i=1 pour dider
	public String Payer(Integer idoffre,Integer i,Client client)
	{
		
		
		if(i==0)
		{
		Offre offre=metierOffre.getOffre(IdOffre);
		 //spécifier le type de l'utilisateur
		 desiredPrice=offre.getPrix_desire();
		 s=(Catalogue_prix) metierStartegie.getCataloguePrix(offre.getPrix_desire(),type);
		 
		 
		 //pres prix
		 prepaid=desiredPrice*(s.getCommission())/100;
		 Double TotalWT=desiredPrice*(s.getTaux())/100;
		 
		 
		 //total du prix incluant  la tva
		 Total=TotalWT+TotalWT*TVA/100;
		 PreTotal=prepaid+prepaid*TVA/100;
		}
		else
		{
			if(client.getType_client().compareTo("Pri")==0)
			{
				if(client.getTranExp().compareTo("Tra")==0)
				{
					type=3;
				}
				else
				{
					type=1;
				}
			}
			else
			{
				if(client.getTranExp().compareTo("Tra")==0)
				{
					type=4;
				}
				else
				{
					type=2;
				}
				
			}
			
			
			
			
			Offre offre=metierOffre.getOffre(idoffre);
			 //spécifier le type de l'utilisateur
			 desiredPrice=offre.getPrix_desire();
			 s=(Catalogue_prix) metierStartegie.getCataloguePrix(offre.getPrix_desire(),type);
			 
			 
			 //pres prix
			 prepaid=desiredPrice*(s.getCommission())/100;
			 Double TotalWT=desiredPrice*(s.getTaux())/100;
			 
			 
			 //total du prix incluant  la tva
			 Total=TotalWT+TotalWT*TVA/100;
			 PreTotal=prepaid+prepaid*TVA/100;
		}
			

		return "Paiement.xhtml";
	}
	
	//informations affichées
	
	public List<String> listeInfo()
	{
		List<String> l=new  ArrayList<String>();
		l.add(" Prepaid Pre-estimated Amount"+PreTotal+".");
		l.add("When auction is closed the Real Rest Amount to be Paid will be defined.");
		l.add("When Real Rest Amount to be paid is made of both Shippers and Carriers Information Contact if each other will be communicated by Move Cargo.");
	    l.add("Shipper and Carrier will make final arrangement between themselves.");
	    return l;
	}
	
	//payer
	public String payer1() throws ParseException
	{
		//tester si l'une des méthodes est sélectionnée
		if(!(bankTransfer || paypal || visa || cash || cheque || westernUnion || moneyGram || wireTransfer ))
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "", "select one payment method"));
			return  "";
		
		}
		else
		{
		String pm = "";
		if(bankTransfer)
			pm="bankTransfer";
		
		if(paypal)
			pm="paypal";
		
		
		if(visa)
			pm="visa";
		
		if(cash)
			pm="cash";
		
		if(cheque)
			pm="cheque";
		
		
		if(westernUnion)
			pm="westernUnion";
		
		if(moneyGram)
			pm="moneyGram";
		
		if(wireTransfer)
			pm="wireTransfer";
  
		
		//offre en question 
		
		Offre offre=metierOffre.getOffre(IdOffre);
		
		//date d'expiration
		   Calendar calendar = Calendar.getInstance();
	    //définir le format de la date
	 
		  calendar.add(calendar.DATE,offre.getAuction() );
		 
	Paiement p=new Paiement("placing bid",pm,calendar.getTime(), false, false, PreTotal, metierOffre.getOffre(IdOffre));
		
		metierPaiement.addPaiement(p);
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "", "succes paiement"));
		
	System.out.println(pm);
		
		if(pm.compareTo("paypal")==0)
		{
			return "Paypal.xhtml";
		}
		else if(pm.compareTo("visa")==0)
		{
			return "paiementSage.xhtml";
		}
		else
		{
			return "infoPaiement.xhtml";
		}
		
		
		}
	}
	
}
